<?php
if(!defined('DCS_ROOT')) {exit('Access Denied');} 

class movie {

	function __construct (){
		$this->url = getUrl();
		$action = $this->url['params']['1'];
		require_once(APP_PATH."model/admin/init.php" );
		if($action == 'add'){
			$this->add();
			exit();
		}elseif($action == 'edit'){
			$this->edit();
			exit();
		}elseif($action == 'del'){
			$this->del();
			exit();
		}else{
			$this->defaultAction();
			exit();
		}
	}
	function defaultAction(){
		$tpl = new Template( "admin/movie/list.htm" );
		require_once(APP_PATH."model/admin/movie.php" );
		$db  = getDB();
		$data = new MovieModel ($db);
		if($this->url['params']['1'] == 'page'){
				$pagetemp = $this->url['params']['2'];		
		}		
		$condition['page']= is_numeric($pagetemp) ? $pagetemp :'1';
		$condition['rows'] = '15';
		$condition['order'] = 'addtime DESC';
		if(isPost()){
			$keyword = htmlClear(addslashes(trim($_POST['keyword'])));
			if(!empty($keyword)){
				$condition['condition'] = " title LIKE '%$keyword%' ";
			}else{
				message('请输入要搜索的关键字！');
			}
			if (strlen($keyword) < 2) {
				message('关键字不能少于2个字符！');
			}
		}
		$movieList = $data->getList($condition);
		foreach($movieList as $k => $v ){
			$movieList[$k]['playtime'] = date("Y-m-d",$v['playtime']);
			$movieList[$k]['endtime'] = date("Y-m-d",$v['endtime']);
			$movieList[$k]['addtime'] = date("Y-m-d",$v['addtime']);
		}
		$tpl->assign('movieList', $movieList);
		$condition['pageUrl'] = '/admin/movie/page/';
		$condition['pageTotal']	= $data->total($condition);
		$tpl->assign("pageParam", makePage($condition));
		$tpl->output();
	}
	function add(){
		require_once(APP_PATH."model/admin/movie.php" );
		$db  = getDB();		
		$data = new MovieModel( $db );
		if(!isPost()){
			$tpl = new Template( "admin/movie/add.htm" );
			$tpl->output();
		}else{
			$var['title'] = trim($_POST['title']);
			$var['playtime'] = trim($_POST['playtime']);
			$var['endtime'] = trim($_POST['endtime']);
			$var['story'] = trim($_POST['story']);
			$var['subdesc'] = trim($_POST['subdesc']);
			$var['descs'] = daddslashes($_POST['descs']);
			$var['mtips'] = daddslashes($_POST['mtips']);

			
			if(!empty($var['playtime'])){
				$var['playtime'] = strtotime($var['playtime']);
			}
			if(!empty($var['endtime'])){
				$var['endtime'] = strtotime($var['endtime']);
			}
			if(!empty($_FILES['thumb']['name'])){
				require_once(CLASS_PATH."class/upload.class.php" );
				$upload = new Upload();	
				$upload->config(array(
					'targetDir' => 'thumb/',
					'saveType' => 0
				));
				$files = $upload->saveFiles('thumb');
				$file_url = $files['0']['filenames'];				
				
				$sourFile = UPLOAD_PATH.'thumb/'.$file_url;
				$mpic = UPLOAD_PATH.'thumb/m/'.$file_url;
				$spic = UPLOAD_PATH.'thumb/s/'.$file_url;
				thumbs($sourFile,$mpic,100,150);
				thumbs($sourFile,$spic,70,100);
			}
			$add = $data->addOne($var['title'],$var['playtime'],$var['endtime'],$var['story'],$var['subdesc'],$var['descs'],$var['mtips'],$file_url);
			if ($add){
				$message = '成功增加电影信息';
			}else{
				$message = '增加电影信息失败';
			}
			message($message);	
		}
	}

	function edit(){
		require_once(APP_PATH."model/admin/movie.php" );
		$db  = getDB();
		$data = new MovieModel( $db );
		$id = $this->url['params']['2'];
		if(!is_numeric($id)){
			message('找不到此条新闻。');
		}
		$movie = $data->getOne($id);		
		if(!isPost()){			
			$tpl = new Template("admin/movie/edit.htm" );
			$movie['playtime'] = date('Y-m-d',$movie['playtime']);
			$movie['endtime'] = date('Y-m-d',$movie['endtime']);			
			$movie['descs'] = htmlspecialchars($movie['descs']);
			$tpl->assign($movie);
			$tpl->output();
		}else{
			$var['title'] = trim($_POST['title']);
			$var['playtime'] = trim($_POST['playtime']);
			$var['endtime'] = trim($_POST['endtime']);
			$var['story'] = trim($_POST['story']);
			$var['subdesc'] = trim($_POST['subdesc']);
			$var['thumbs'] = trim($_POST['thumbs']);
			$var['mtips'] = daddslashes($_POST['mtips']);
			$var['descs'] = daddslashes($_POST['descs']);
			if(!empty($var['playtime'])){
				$var['playtime'] = strtotime($var['playtime']);
			}
			if(!empty($var['endtime'])){
				$var['endtime'] = strtotime($var['endtime']);
			}

			if(!empty($_FILES['thumb']['name'])){
				require_once(CLASS_PATH."class/upload.class.php" );
				$upload = new Upload();	
				$upload->config(array(
					'targetDir' => 'thumb/',
					'saveType' => 0
				));
				$files = $upload->saveFiles('thumb');
				$file_url = $files['0']['filenames'];				
				
				$sourFile = UPLOAD_PATH.'thumb/'.$file_url;
				$mpic = UPLOAD_PATH.'thumb/m/'.$file_url;
				$spic = UPLOAD_PATH.'thumb/s/'.$file_url;
				thumbs($sourFile,$mpic,100,150);
				thumbs($sourFile,$spic,70,100);
			}else{
				$file_url = $var['thumbs'];
			}

			$edit = $data->editOne($id,$var['title'],$var['playtime'],$var['endtime'],$var['story'],$var['subdesc'],$var['descs'],$var['mtips'],$file_url);
			if ($edit){
				$message = '修改电影信息成功。';
			}else{
				$message = '修改电影信息失败！';
			}
			message($message);			
		}
	}


	function del(){
		require_once(APP_PATH."model/admin/movie.php" );
		$db  = getDB();
		$data = new MovieModel( $db );
		$id = $this->url['params']['2'];
		if(!is_numeric($id)){
			error('找不到此条电影信息。');
		}
		$del = $data->delOne($id);
		if ($del){
			$message = '删除电影成功。';
		}else{
			$message = '删除电影失败！';
		}
		exit($message);
	}

}
?>
